package com.workhours.controller;

import com.workhours.dto.RestRecordRequest;
import com.workhours.service.RestRecordService;
import com.workhours.vo.Result;
import com.workhours.vo.UserStatusResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 休息记录控制器
 *
 * @author WorkHours
 * @since 2025-09-19
 */
@Tag(name = "休息记录管理", description = "休息记录的增删改查操作")
@RestController
@RequestMapping("/api/rest")
public class RestRecordController {
    
    private static final Logger log = LoggerFactory.getLogger(RestRecordController.class);
    
    @Autowired
    private RestRecordService restRecordService;
    
    /**
     * 添加休息记录
     *
     * @param request 休息记录请求
     * @return 新增的休息记录
     */
    @Operation(summary = "添加休息记录", description = "为当前用户添加新的休息记录")
    @PostMapping
    public Result<UserStatusResponse.RestRecordVO> addRestRecord(@Valid @RequestBody RestRecordRequest request) {
        try {
            log.info("收到添加休息记录请求: {}", request);
            UserStatusResponse.RestRecordVO response = restRecordService.addRestRecord(request);
            log.info("休息记录添加成功: {}", response);
            return Result.success("添加成功", response);
        } catch (Exception e) {
            log.error("添加休息记录失败", e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新休息记录
     *
     * @param id 记录ID
     * @param request 休息记录请求
     * @return 更新后的休息记录
     */
    @Operation(summary = "更新休息记录", description = "更新指定ID的休息记录")
    @PutMapping("/{id}")
    public Result<UserStatusResponse.RestRecordVO> updateRestRecord(
            @Parameter(description = "休息记录ID") @PathVariable Long id,
            @Valid @RequestBody RestRecordRequest request) {
        try {
            log.info("收到更新休息记录请求: id={}, request={}", id, request);
            UserStatusResponse.RestRecordVO response = restRecordService.updateRestRecord(id, request);
            log.info("休息记录更新成功: {}", response);
            return Result.success("更新成功", response);
        } catch (Exception e) {
            log.error("更新休息记录失败", e);
            return Result.error("更新失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除休息记录
     *
     * @param id 记录ID
     * @return 删除结果
     */
    @Operation(summary = "删除休息记录", description = "删除指定ID的休息记录")
    @DeleteMapping("/{id}")
    public Result<?> deleteRestRecord(@Parameter(description = "休息记录ID") @PathVariable Long id) {
        try {
            log.info("收到删除休息记录请求: id={}", id);
            restRecordService.deleteRestRecord(id);
            log.info("休息记录删除成功: id={}", id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error("删除休息记录失败", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}
